package com.aurora_manito.xiaomi.service.impl;

import com.aurora_manito.xiaomi.entity.Goods;
import com.aurora_manito.xiaomi.entity.GoodsType;
import com.aurora_manito.xiaomi.entity.PageBean;

import com.aurora_manito.xiaomi.mapper.GoodsMapper;
import com.aurora_manito.xiaomi.mapper.GoodsimageMapper;
import com.aurora_manito.xiaomi.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.Id;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Aurora_Manito
 * @since 2020-05-27
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    private QueryWrapper<Goods> goodQueryWapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private  GoodsimageMapper goodsimageMapper;
    public List<Goods> getGoodByGoodTypeId(int i) {
        Goods goods=new Goods();
        goods.setGoodtypeid(i);
        goodQueryWapper=new QueryWrapper<Goods>().setEntity(goods);
        List<Goods> goodsList=goodsMapper.selectList(goodQueryWapper);
        return goodsList;
    }

    //pageBeam调用分页查询 返回pageBean的主方法
    public PageBean<Goods> getGoodByTypeIDPageBean(String typeId, PageBean<Goods> pageBean) {
        Goods goods=new Goods();
        goods.setGoodtypeid(Integer.parseInt(typeId));
        //查询多少条记录
        int totalRecordCount=(int)countAllReordByExampleEq(goods);
        //设置商品总条数
        pageBean.setTotalRecordCount(totalRecordCount);

        //4.获取当前页要显示的数据集，将其赋值给pageBeand对象
        int pageSize = pageBean.getPageSize();
        List<Goods> pageList=findAllGoodsByPageBeanAndGoodExample(pageBean.getCurrentPageNO(),pageSize,goods);
        System.out.println("查询出来的"+pageList);
        return new PageBean<Goods>(pageBean.getCurrentPageNO(),pageSize,totalRecordCount,pageList);
//        for (Goods goods1 : pageList) {
//            System.out.println(goods1);
//        }
//        pageBean.setPageList(pageList);
//        return pageBean;

    }

    //2020 6 4 查看购物车有问题
    @Override
    public Goods getGoodsById(String id) {
        Goods goods=new Goods();
        goods=goodsMapper.AselectGoodByID(Integer.parseInt(id));
        return goods;
    }

    private List<Goods> findAllGoodsByPageBeanAndGoodExample(int currentPageNO, int pageSize,Goods goods){

        //select * from tb_goods where typeid=? order by id limit ?,?  从第50个开始查5个
        Map<String,Object> goodsCondition=CreateGoodsMapByExample(goods);
        int StartRowNumber=(currentPageNO-1)*pageSize;
        List<Goods> goodsList=goodsMapper.selectList(new QueryWrapper<Goods>().allEq(goodsCondition,false).orderByAsc("id").last("limit "+StartRowNumber+","+pageSize));
        for (Goods goods1 : goodsList) {
            goods1.setGoodsimageList(goodsimageMapper.AfindImageByGoodId(goods1.getId()));
        }
        return goodsList;

        //mybatis版本
//        ge.createCriteria().andGoodtypeidEqualTo(goods.getGoodtypeid());
//        ge.setOrderByClause("id ASC");
//        System.out.println("find All page 当前页号"+currentPageNO+"页面大小"+pageSize+goods);
        //2020/6/2之前 Interesting！
//写了扩展查询
//        goodQueryWapper.allEq(goodsCondition,false).orderByAsc("id");
//        //Page page=new Page(currentPageNO,pageSize);
//        goodsMapper.selectPage(page,goodQueryWapper);
//        //获取当前分页总数
//        return page.getRecords() ;

        //2020/6/2重新改写了查询的方法 成功，
//        System.out.println("******************************华丽的分割线*********************************");
//        List<Goods>  goodsLists = goodsMapper.selectList(new QueryWrapper<Goods>().allEq(goodsCondition, false).orderByAsc("id").last("limit " + currentPageNO + "," + pageSize));
//        for (Goods goodsList : goodsLists) {
//            System.out.println(goodsList);
//        }
//        System.out.println("******************************华丽的分割线（低）*********************************");
//        return goodsMapper.selectList(new QueryWrapper<Goods>().allEq(goodsCondition,false).orderByAsc("id").last("limit "+currentPageNO+","+pageSize));

//        //2020/6/2重新改写了查询的方法 成功，
//        Page goodpage = goodsMapper.selectPage(new Page(currentPageNO, pageSize), new QueryWrapper<Goods>().allEq(goodsCondition, false).orderByAsc("id"));
//        //获取当前分页总数
//        return goodpage.getRecords() ;
    }

    //分页查询获得商品总数通过typeID暂不做优化（优化)
    private long countAllReordByExampleEq(Goods goods) {
        Map<String,Object> goodsCondition=CreateGoodsMapByExample(goods);
        QueryWrapper<Goods> goodQueryWapper=new QueryWrapper();
        goodQueryWapper.allEq(goodsCondition,false);
        return  goodsMapper.selectCount(goodQueryWapper);
    }

    private Map<String,Object> CreateGoodsMapByExample(Goods goods)
    {
        Map<String,Object> goodsCondition=new HashMap<>();
        goodsCondition.put("id",goods.getId());
        goodsCondition.put("goodtypeid",goods.getGoodtypeid());
        goodsCondition.put("name",goods.getName());
        goodsCondition.put("pictureid",goods.getPictureid());
        goodsCondition.put("price",goods.getPrice());
        goodsCondition.put("putdate",goods.getPutdate());
        goodsCondition.put("star",goods.getStar());
        return goodsCondition;
    }
}
